package com.cuput.partybuilding.contact.adapter;

import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cuput.partybuilding.contact.R;
import com.cuput.partybuilding.contact.bean.FunctionInfo;

import java.util.ArrayList;

/**
 * Created by wei on 2017-05-10.
 * 联系人功能项适配器
 */

public class ContactFunctionsAdapter extends BaseAdapter {
    private Context mContext;
    private ArrayList<FunctionInfo> functionInfos;

    public ContactFunctionsAdapter(Context context, ArrayList<FunctionInfo> functionInfos) {
        super();
        this.mContext = context;
        this.functionInfos = functionInfos;
    }

    public void setFunctionInfos(ArrayList<FunctionInfo> functionInfos) {
        this.functionInfos = functionInfos;
    }

    @Override
    public int getCount() {
        return functionInfos == null ? 0 : functionInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return functionInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_contact_list, parent, false);
        }

        FunctionInfo functionInfo = functionInfos.get(position);

        LinearLayout portrait = (LinearLayout) convertView.findViewById(R.id.portrait);
        GradientDrawable gradientDrawable = (GradientDrawable) portrait.getBackground();
        gradientDrawable.setColor(mContext.getResources().getColor(functionInfo.getPortraitBgColorRes()));

        ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
        icon.setImageResource(functionInfo.getPortraitImageRes());

        TextView description = (TextView) convertView.findViewById(R.id.description);
        description.setText(functionInfo.getDescription());

        if (position == functionInfos.size() - 1) {
            View line = convertView.findViewById(R.id.line);
            line.setVisibility(View.GONE);
        }

        return convertView;
    }
}
